package com.jfreer.mybatis.expand.xmlmapper;

import com.jfreer.mybatis.expand.xmlmapper.elements.*;
import org.mybatis.generator.api.FullyQualifiedTable;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.XMLMapperGenerator;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator;

import static org.mybatis.generator.internal.util.messages.Messages.getString;

/**
 * User: landy
 * Date: 14-6-23
 * Time: 上午9:02
 */
public class FreeworkXMLMapperGenerator extends XMLMapperGenerator {
    protected XmlElement getSqlMapElement() {
        FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
        progressCallback.startTask(getString(
                "Progress.12", table.toString())); //$NON-NLS-1$
        XmlElement answer = new XmlElement("mapper"); //$NON-NLS-1$
        String namespace = introspectedTable.getMyBatis3SqlMapNamespace();
        answer.addAttribute(new Attribute("namespace", //$NON-NLS-1$
                namespace));

        context.getCommentGenerator().addRootComment(answer);

        addResultMapWithoutBLOBsElement(answer);
        addResultMapWithBLOBsElement(answer);
        addExampleWhereClauseElement(answer);
        addMyBatis3UpdateByExampleWhereClauseElement(answer);
        addBaseColumnListElement(answer);
        addBlobColumnListElement(answer);
        addSelectByExampleWithBLOBsElement(answer);
        addSelectByExampleWithoutBLOBsElement(answer);
        addSelectByPrimaryKeyElement(answer);
        addDeleteByPrimaryKeyElement(answer);
        addDeleteByExampleElement(answer);
        addInsertElement(answer);
        addInsertSelectiveElement(answer);
        addCountByExampleElement(answer);
        addUpdateByExampleSelectiveElement(answer);
        addUpdateByExampleWithBLOBsElement(answer);
        addUpdateByExampleWithoutBLOBsElement(answer);
        addUpdateByPrimaryKeySelectiveElement(answer);
        addUpdateByPrimaryKeyWithBLOBsElement(answer);
        addUpdateByPrimaryKeyWithoutBLOBsElement(answer);

        //2014-06-23 landy 增加额外方法
        addSelectAllElement(answer);
        addCountAllElement(answer);
        addDeleteAllElement(answer);
        //2014-06-28 landy 增加额外方法
        addDeleteBySelectiveElement(answer);
        addSelectBySelectiveElement(answer);
        addInsertInBatchElement(answer);
        addDeleteInBatchElement(answer);

        return answer;
    }

    private void addDeleteInBatchElement(XmlElement answer) {
        AbstractXmlElementGenerator elementGenerator = new DeleteInBatchElementGenerator();
        initializeAndExecuteGenerator(elementGenerator, answer);
    }

    private void addInsertInBatchElement(XmlElement answer) {
        AbstractXmlElementGenerator elementGenerator = new InsertInBatchElementGenerator();
        initializeAndExecuteGenerator(elementGenerator, answer);
    }

    private void addSelectBySelectiveElement(XmlElement answer) {
        AbstractXmlElementGenerator elementGenerator = new SelectBySelectiveElementGenerator();
        initializeAndExecuteGenerator(elementGenerator, answer);
    }

    private void addDeleteBySelectiveElement(XmlElement answer) {
        AbstractXmlElementGenerator elementGenerator = new DeleteBySelectiveElementGenerator();
        initializeAndExecuteGenerator(elementGenerator, answer);
    }

    private void addCountAllElement(XmlElement parentElement) {
        AbstractXmlElementGenerator elementGenerator = new CountAllElementGenerator();
        initializeAndExecuteGenerator(elementGenerator, parentElement);
    }

    private void addSelectAllElement(XmlElement parentElement) {
        AbstractXmlElementGenerator elementGenerator = new SelectAllElementGenerator();
        initializeAndExecuteGenerator(elementGenerator, parentElement);
    }

    private void addDeleteAllElement(XmlElement parentElement) {
        AbstractXmlElementGenerator elementGenerator = new DeleteAllElementGenerator();
        initializeAndExecuteGenerator(elementGenerator, parentElement);
    }
}
